package cn.edu.njnu.opengmpback.Modeling.controller;


import cn.edu.njnu.opengmpback.Modeling.entity.dto.CreateProjectDTO;
import cn.edu.njnu.opengmpback.Modeling.entity.dto.ProjectPageDTO;
import cn.edu.njnu.opengmpback.Modeling.entity.dto.UpdateProjectDTO;
import cn.edu.njnu.opengmpback.Modeling.service.ProjectService;
import cn.edu.njnu.opengmpback.common.utils.JsonResult;
import cn.edu.njnu.opengmpback.common.utils.ResultUtils;
import cn.edu.njnu.opengmpback.user.entity.UserVo;
import cn.edu.njnu.opengmpback.user.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@RestController
@RequestMapping(value = "/project")
public class ProjectController {

    @Autowired
    ProjectService projectService;

    @Autowired
    UserService userService;

    @ApiOperation(value = "创建项目")
    @PostMapping("/saveProject")
    public JsonResult saveProject(@RequestBody CreateProjectDTO createProjectDTO,
                                  HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return projectService.saveProject(createProjectDTO, userId);
    }

    @ApiOperation(value = "更新项目")
    @PostMapping("/updateProject")
    public JsonResult updateProject(@RequestBody UpdateProjectDTO updateProjectDTO){
        return projectService.updateProject(updateProjectDTO);
    }

    @ApiOperation(value = "删除项目")
    @PostMapping("/deleteProject/{projectId}")
    public JsonResult deleteProject(@PathVariable String projectId, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return projectService.deleteProject(projectId, userId);
    }

    @ApiOperation(value = "邀请成员加入项目")
    @PostMapping("/joinProject/{projectId}")
    public JsonResult joinProject(@PathVariable String projectId, @RequestBody List<String> emails, HttpServletRequest request){
        Object userState = userService.userState(request);
        if(userState.equals(false)){
            return ResultUtils.error(-1,"用户未登录");
        }
        UserVo userVo = (UserVo)userState;
        String userId = userVo.getUserId();
        return projectService.joinProject(projectId, emails, userId);
    }

    @ApiOperation(value = "成员退出项目")
    @PostMapping("/quitProject/{projectId}/{userIds}")
    public JsonResult quitProject(@PathVariable String projectId, @PathVariable List<String> userIds){
        return projectService.quitProject(projectId, userIds);
    }

    @ApiOperation(value = "查询项目列表")
    @PostMapping("/getProjectList")
    public JsonResult inquiryByPage(@RequestBody ProjectPageDTO projectPageDTO){
        return projectService.inquiryByPage(projectPageDTO);
    }

    @ApiOperation(value = "查询项目信息")
    @GetMapping("/getProjectById/{projectId}")
    public JsonResult getProjectById(@PathVariable String projectId){
        return projectService.getProjectById(projectId);
    }

    @ApiOperation(value = "查询项目成员信息")
    @GetMapping("/getMemberList/{projectId}")
    public JsonResult getMemberList(@PathVariable String projectId){
        return projectService.getMemberList(projectId);
    }
}
